import requests
import unittest

class A3mall(unittest.TestCase):
    # 提取的参数
    # 登录用户的token值
    token=None
    # 商品id
    spid =None
    # 销售价格
    sell_price =None

    def test_a_A3mall_login(self):
        url = 'http://10.59.9.14:7003/api/public/login'
        json = {"username": "13057972962", "password": "123456"}
        login1 = requests.post(url=url, json=json)
        print(login1.json())
        A3mall.token = login1.json()['data']['token']
        print(A3mall.token)

    def test_b_A3mall_info(self):
        url = 'http://10.59.9.14:7003/api/ucenter/info'
        headers = {"Auth-Token":f"Bearer {A3mall.token}"}
        info = requests.get(url=url,headers=headers)
        print(info.json())
    # send 签到接口 每天只能签到一次 再次调用接口状态码虽然为0但是算成功
    def test_c_A3mall_info(self):
        url = 'http://10.59.9.14:7003/api/sign/send'
        headers = {"Auth-Token": "Bearer " + A3mall.token}
        send = requests.get(url=url, headers=headers)
        print(send.json())
    def test_d_A3mall_search(self):
        url = 'http://10.59.9.14:7003/api/search'
        search = requests.get(url=url)
        print(search.json())

    def test_e_A3mall_search_list(self):
        url = 'http://10.59.9.14:7003/api/search/list?page=1&keywords=%E7%83%AD%E9%A3%8E%E6%9C%BA&type=0&sort=1'
        search_list = requests.get(url=url)
        print(search_list.json())
        A3mall.spid = search_list.json()['data']['list'][0]['id']
        print(A3mall.spid)

    def test_f_A3mall_view(self):
        url = 'http://10.59.9.14:7003/api/goods/view?id='+str(A3mall.spid)
        view = requests.get(url=url)
        print(view.json())
        A3mall.sell_price=view.json()['data']['goods']['sell_price']
        print(A3mall.sell_price)

    def test_g_A3mall_add(self):
        url = 'http://10.59.9.14:7003/api/cart/add'
        headers = {"Auth-Token": "Bearer " + A3mall.token}
        json= {"id":A3mall.spid,"sku_id":"","num":1}
        add = requests.post(url=url,headers=headers,json=json)
        print(add.json())

    card_id =None
    def test_h_A3mall_card(self):
        url = 'http://10.59.9.14:7003/api/cart?page=1'
        headers = {"Auth-Token": "Bearer " + A3mall.token}
        card = requests.get(url=url,headers=headers)
        print(card.json())
        A3mall.card_id=card.json()['data']['list'][0]['id']
        print(A3mall.card_id)
    def test_i_A3mall_confirm(self):
        url = 'http://10.59.9.14:7003/api/order/confirm?id='+str(A3mall.card_id)+'&type=cart&shipping_type=1'
        headers = {"Auth-Token": "Bearer " + A3mall.token}
        confirm = requests.get(url=url,headers=headers)
        print(confirm.json())

    order_id = None
    def test_j_A3mall_create(self):
        url = 'http://10.59.9.14:7003/api/order/create'
        headers = {"Auth-Token": "Bearer " + A3mall.token}
        json = {"id":A3mall.card_id,"type":"cart","address_id":182,"store_id":"0","shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":"http://10.59.9.14:7003/wap/pages/cart/confirm?id=12&type=cart"}

        create = requests.post(url=url,headers=headers,json=json)
        print(create.json())
        A3mall.order_id=create.json()['data']['order_id']
        print(A3mall.order_id)

    def test_k_A3mall_detail(self):
        url = 'http://10.59.9.14:7003/api/order/detail'
        headers = {"Auth-Token": "Bearer " + A3mall.token}
        json ={"id":A3mall.order_id}
        detail = requests.post(url=url,headers=headers,json=json)
        print(detail.json())



if __name__ == '__main__':
    unittest.main()
